package main

import (
	"fmt"
	"io"
	"net/http"
)

const form = `
<html>
<body>
<form action="#" method="post" name="bar">
    <input type="text" name="in" />
    <input type="text" name="in" />
    <input type="submit" value="Submit" />
</form>
</body>
</html>`

func FormServer(w http.ResponseWriter, request *http.Request) {
	w.Header().Set("content-Type", "text/html")
	switch request.Method {
	case "GET":
		io.WriteString(w, form)
	case "POST":
		request.ParseForm()
		io.WriteString(w, request.Form["in"][0])  // 注意上面的2个input的name是一样的
		io.WriteString(w, request.Form["in"][1])  // 所以这是一个数组
		io.WriteString(w, "</br>")
		io.WriteString(w, request.FormValue("in"))  // 一般去一个值，就用这个方法
	}
}

func main() {
	http.HandleFunc("/form", FormServer)
	if err := http.ListenAndServe(":8000", nil); err != nil {
		fmt.Println("监听端口ERROR:", err)
	}
}